package cn.com.wdjedu.androidapp.wjeduandroid.support.lib.mvputils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedConfig {
	//应用上下文
	private Context mContext = null;
	private SharedPreferences mShare = null;
	public SharedConfig(Context context) throws Exception
	{
		if(context==null)
		{
			throw new Exception("上下文参数不能为空");
		}
		mContext=context;
		mInstance=this;
		mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
		if(mShare==null)
		{
			throw new Exception("获取共享配置失败");
		}
	}
	
	private  static SharedConfig mInstance=null;
	//获取单体实例
	public static SharedConfig getInstance(Context context) throws Exception
	{
		if(mInstance==null)
		{
			mInstance=new SharedConfig(context);
		}
		return mInstance;
	}
	//设置整数值
	public void setIntValue(String keyName,int value)
	{
		if(mShare==null)
		{
			mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
		}
		if(mShare!=null)
		{
			 Editor edit = mShare.edit();
	 	     edit.putInt(keyName, value);
	 	     edit.commit();  
		}
	}
	//获取整数值
	public int getIntValue(String keyName,int defaultValue)
	{
		if(mShare==null)
		{
			mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
		}
		if(mShare!=null)
		{
			defaultValue=mShare.getInt(keyName, defaultValue);
		}
		return defaultValue;
	}
	//设置字符串值
	public void setStringValue(String keyName,String value)
	{
		if(mShare==null)
		{
			mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
		}
		if(mShare!=null)
		{
			 Editor edit = mShare.edit();
	 	     edit.putString(keyName, value);
	 	     edit.commit();  
		}
	}
	//获取字符串值
	public String getStringValue(String keyName,String defaultValue)
	{
		if(mShare==null)
		{
			mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
		}
		if(mShare!=null)
		{
			defaultValue=mShare.getString(keyName, defaultValue);
		}
		return defaultValue;
	}
	
	//设置字符串值
	public void setBooleanValue(String keyName,boolean value)
	{
			if(mShare==null)
			{
				mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
			}
			if(mShare!=null)
			{
				 Editor edit = mShare.edit();
		 	     edit.putBoolean(keyName, value);
		 	     edit.commit();  
			}
	}
	//获取字符串值
	public boolean getBooleanValue(String keyName,boolean defaultValue)
	{
			if(mShare==null)
			{
				mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
			}
			if(mShare!=null)
			{
				defaultValue=mShare.getBoolean(keyName, defaultValue);
			}
			return defaultValue;
	}

	//设置用户id
	public void setUserId(String userId,String value)
	{
			if(mShare==null)
			{
				mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
			}
			if(mShare!=null)
			{
				 Editor edit = mShare.edit();
		 	     edit.putString(userId, value);
		 	     edit.commit();
			}
	}
	//获取用户id
	public String getUserId(String userId,String defaultValue)
	{
			if(mShare==null)
			{
				mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
			}
			if(mShare!=null)
			{
				defaultValue=mShare.getString(userId, defaultValue);
			}
			return defaultValue;
	}
	//设置用户名
	public void setUserName(String name,String value)
	{
			if(mShare==null)
			{
				mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
			}
			if(mShare!=null)
			{
				 Editor edit = mShare.edit();
		 	     edit.putString(name, value);
		 	     edit.commit();
			}
	}
	//获取用户名
	public String getUserName(String name,String defaultValue)
	{
			if(mShare==null)
			{
				mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
			}
			if(mShare!=null)
			{
				defaultValue=mShare.getString(name, defaultValue);
			}
			return defaultValue;
	}

	public void setUserAvatar(String photoUrl, String value) {
		if(mShare==null)
		{
			mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
		}
		if(mShare!=null)
		{
			Editor edit = mShare.edit();
			edit.putString(photoUrl, value);
			edit.commit();
		}
	}
	public String getUserAvatar(String photoUrl, String defaultValue) {
		if(mShare==null)
		{
			mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
		}
		if(mShare!=null)
		{
			defaultValue=mShare.getString(photoUrl, defaultValue);
		}
		return defaultValue;
	}

	//设置用户key--->加密的密码
	public void setUserKey(String key,String value)
	{
			if(mShare==null)
			{
				mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
			}
			if(mShare!=null)
			{
				 Editor edit = mShare.edit();
		 	     edit.putString(key, value);
		 	     edit.commit();
			}
	}
	//获取用户key--->加密的密码
	public String getUserKey(String key,String defaultValue)
	{
			if(mShare==null)
			{
				mShare= mContext.getSharedPreferences("sharedconfig", Context.MODE_PRIVATE);
			}
			if(mShare!=null)
			{
				defaultValue=mShare.getString(key, defaultValue);
			}
			return defaultValue;
	}


}
